package com.team2.insulatingtools.modular.tools.controller;

import com.team2.insulatingtools.modular.tools.entity.Category;
import com.team2.insulatingtools.modular.tools.pojo.request.CategoryRequest;
import com.team2.insulatingtools.modular.tools.service.CategoryService;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 控制器
 *
 * @author team2
 * @date 2023/04/07 16:03
 */
@RestController
@ApiResource(name = "")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 添加
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    @PostResource(name = "添加", path = "/category/add")
    public ResponseData<Category> add(@RequestBody @Validated(CategoryRequest.add.class) CategoryRequest categoryRequest) {
        categoryService.add(categoryRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    @PostResource(name = "删除", path = "/category/delete")
    public ResponseData<?> delete(@RequestBody @Validated(CategoryRequest.delete.class) CategoryRequest categoryRequest) {
        categoryService.del(categoryRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    @PostResource(name = "编辑", path = "/category/edit")
    public ResponseData<?> edit(@RequestBody @Validated(CategoryRequest.edit.class) CategoryRequest categoryRequest) {
        categoryService.edit(categoryRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看详情
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    @GetResource(name = "查看详情", path = "/category/detail")
    public ResponseData<Category> detail(@Validated(CategoryRequest.detail.class) CategoryRequest categoryRequest) {
        return new SuccessResponseData<>(categoryService.detail(categoryRequest));
    }

    /**
     * 获取列表
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    @GetResource(name = "获取列表", path = "/category/list")
    public ResponseData<List<Category>> list(CategoryRequest categoryRequest) {
        return new SuccessResponseData<>(categoryService.findList(categoryRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    @GetResource(name = "分页查询", path = "/category/page")
    public ResponseData<PageResult<Category>> page(CategoryRequest categoryRequest) {
        return new SuccessResponseData<>(categoryService.findPage(categoryRequest));
    }

}
